package org.feng.entity;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import org.feng.entity.result.SysMenuTreeResult;
import org.feng.enums.SysMenuCategoryEnum;

import java.io.Serializable;
import java.util.List;

/**
 * 菜单表
 *
 * @author : FengShuai
 * @since : 2024/6/27 9:24
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("sys_menu")
@Schema(name = "菜单表")
public class SysMenu extends Model<SysMenu> implements Serializable {

    private static final long serialVersionUID = 1L;
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    @Schema(title = "主键")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;
    @Schema(title = "父id")
    @TableField(value = "parent_id", updateStrategy = FieldStrategy.ALWAYS)
    @JsonSerialize(using = ToStringSerializer.class)
    private Long parentId;
    @Schema(title = "名称")
    private String name;
    @Schema(title = "类型")
    private Integer type;
    @Schema(title = "权限代码")
    private String authCode;
    @Schema(title = "组件路径")
    private String path;
    @Schema(title = "图标")
    private String icon;
    @Schema(title = "排序")
    private Integer sort;
    @Schema(title = "菜单类型")
    private SysMenuCategoryEnum category;
    @Schema(title = "缓存")
    private Boolean keepAlive;
    @TableLogic
    private Boolean deleted;

    public static SysMenu createButton(String parentId) {
        SysMenu sysMenu = new SysMenu();
        sysMenu.setCategory(SysMenuCategoryEnum.BUTTON);
        sysMenu.setParentId(Long.valueOf(parentId));
        return sysMenu;
    }

    public static String buildAuthCode(String path) {
        if (StrUtil.isNotEmpty(path)) {
            String[] split = path.split("/");
            String authCode = split[split.length - 1];
            if ("/".equals(authCode) && split.length > 2) {
                authCode = split[split.length - 2];
            }
            return authCode;
        }
        return null;
    }

}
